package com.jshcbd.mvp_kt_demo.utils;


import android.util.Base64;

import java.nio.charset.StandardCharsets;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * AES加密解密算法
 */
public class AesEncryptionUtil {
    /*** 算法/模式/填充 */
    private static final String CipherMode = "AES/CBC/PKCS5PADDING";

    /*** KEY */
    private static final String KEY = "12f862d21dcfeafb57bckfrrt5yushen";

    /*** IV */
    private static final String IV = "shen2018zhou8888";

    private static final int KEYMany = 16;

    /**
     * 加密
     *
     * @param value 加密的字符串
     * @return 加密后的字符串
     */
    public static String encrypt(String value) {
        try {
            IvParameterSpec iv = new IvParameterSpec(IV.getBytes(StandardCharsets.UTF_8));
            SecretKeySpec skeySpec = new SecretKeySpec(KEY.getBytes(StandardCharsets.UTF_8), "AES");

            Cipher cipher = Cipher.getInstance(CipherMode);
//            cipher.init(Cipher.ENCRYPT_MODE, createKey(KEY), iv);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            byte[] encrypted = cipher.doFinal(value.getBytes());
            return Base64.encodeToString(encrypted, Base64.DEFAULT);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    /**
     * 解密
     *
     * @param encrypted 需要解密的字符串
     * @return 原来的字符串
     */
    public static String decrypt(String encrypted) {
        try {
            IvParameterSpec iv = new IvParameterSpec(IV.getBytes(StandardCharsets.UTF_8));
            SecretKeySpec skeySpec = new SecretKeySpec(KEY.getBytes(StandardCharsets.UTF_8), "AES");

            Cipher cipher = Cipher.getInstance(CipherMode);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

            byte[] original = cipher.doFinal(Base64.decode(encrypted, Base64.DEFAULT));
            return new String(original);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    /**
     * 创建密钥 16位
     *
     * @param key 秘钥
     * @return 规范后的秘钥
     */
    private static SecretKeySpec createKey(String key) {
        byte[] data = null;
        if (key == null) {
            key = "";
        }

        StringBuilder sb = new StringBuilder(KEYMany);
        sb.append(key);
        while (sb.length() < KEYMany) {
            sb.append("0");
        }
        if (sb.length() > KEYMany) {
            sb.setLength(KEYMany);
        }

        data = sb.toString().getBytes(StandardCharsets.UTF_8);
        return new SecretKeySpec(data, "AES");
    }

    /**
     * 创建IV
     *
     * @param IV iv
     * @return 规范后的iv
     */
    private static IvParameterSpec createIV(String IV) {
        byte[] data = null;
        if (IV == null) {
            IV = "";
        }

        StringBuilder sb = new StringBuilder(16);
        sb.append(IV);
        while (sb.length() < 16) {
            sb.append("0");
        }
        if (sb.length() > 16) {
            sb.setLength(16);
        }

        data = sb.toString().getBytes(StandardCharsets.UTF_8);
        return new IvParameterSpec(data);
    }
}